/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.DateUtils;
import com.apache.uct.common.entity.Sys;
import com.apache.uct.manager.SysManager;

import java.util.Date;

/**
 * description:  按属性获取机构信息
 *
 * @author iussoft 创建时间：2016-3-30
 */
public class SysEntityPlugin implements PluginConnector {

    private IDao sysDao;

    private SysManager sysManager;

    /**
     * TODO 按属性获取系统信息(sysId,sysEname).  <br>
     * 参数说明:<br>
     * propertyName:属性名称<br>
     * propertyValue:属性值
     */
    public Object execute(ParamsVo vo) throws Exception {
        String key = String.valueOf(vo.getParams("method"));
        ResultEntity result = new ResultEntity();
        result.setResult("true");
        if (Validator.isNull(key)) {
            selectDefault(result, vo);
        } else {
            if ("sysData".equalsIgnoreCase(key)) {
                selectSysList(result, vo);
            } else if ("sysSave".equalsIgnoreCase(key)) {
                sysSave(result, vo);
            }
        }
        return result;
    }

    private void sysSave(ResultEntity result, ParamsVo param) {
        ParamsVo<Sys> vo = new ParamsVo<Sys>();
        long times = DateUtils.toLong(new Date());
        String userEname = String.valueOf(param.getParams("userEname"));
        Sys item = new Sys();
        item = (Sys) ClassToolsUtil.getInstance().convert2Bean(param.getParams(), item);
        if (Validator.isNotNull(item.getSysId())) {//对系统信息修改
            item.setUpdateTime(times);
            item.setUpdateUser(userEname);
            vo.setObj(item);
            boolean mark = sysManager.editInfo(vo);
            if (!mark) {
                result.setMessage("系统信息修改失败！");
            } else {
                result.setEntity(mark);
                result.setMessage("系统信息修改成功！");
            }
        } else {//添加系统信息
            item.setCreateUser(userEname);
            item.setCreateTime(times);
            vo.setObj(item);
            vo.setKey("checkSysEname");
            String flag = String.valueOf(sysManager.execute(vo));
            if ("true".equals(flag)) {
                String id = sysManager.saveInfo(vo);
                if (Validator.isNull(id)) {
                    result.setMessage("系统信息添加失败！");
                } else {
                    result.setEntity(id);
                    result.setMessage("系统信息添加成功！");
                }
            } else {
                result.setMessage("该系统标识已经存在，操作失败！");
            }
        }
    }

    private void selectDefault(ResultEntity result, ParamsVo vo) {
        String propertyName = String.valueOf(vo.getParams("propertyName"));
        String propertyValue = String.valueOf(vo.getParams("propertyValue"));

        try {
            if ("sysId".equals(propertyName) || "sysEname".equals(propertyName)) {
                MethodParam param = new MethodParam("ById", "", "", Sys.class.getName());
                Sys sys = null;
                if ("sysEname".equals(propertyName)) {
                    param.setKey("BySysEname");
                    param.setParams(propertyName, propertyValue);
                    sys = (Sys) sysDao.selectSingle(param);
                } else {
                    param.setInfoId(propertyValue);
                    sys = (Sys) sysDao.selectById(param);
                }
                result.setEntity(sys);
                result.setMessage("系统信息查询成功！");
            } else {
                result.setMessage("不支持属性" + propertyName + "的查询！");
            }
        } catch (Exception e) {
            result.setResult("false");
            result.setMessage("系统信息查询失败！");
        }
    }

    private void selectSysList(ResultEntity result, ParamsVo vo) {
        MethodParam param = setMethodParams(vo, 2);
        if (Validator.isEmpty(param)) {
            result.setMessage("查询条件为空或不存在相关信息");
            return;
        }

        int pageSize = Integer.valueOf(Validator
                .getDefaultStr(String.valueOf(vo.getParams("pageSize")),
                        Validator.getDefaultStr(String.valueOf(vo.getParams("rows")), "0")));
        int pageIndex = Integer.valueOf(Validator
                .getDefaultStr(String.valueOf(vo.getParams("pageIndex")),
                        Validator.getDefaultStr(String.valueOf(vo.getParams("page")), "0")));
        param.setOrderby(" order_num asc,create_time desc");
        //param.setParams("orderby", " order_num asc,create_time desc");
        if (pageSize == 0 && pageIndex == 0) {
            result.setEntity(sysDao.select(param));
        } else {
            param.setPageIndex(pageIndex);
            param.setPageSize(pageSize);
            Page page = sysDao.pageSelect(param);
            result.setEntity(page);
        }
    }

    private MethodParam setMethodParams(ParamsVo vo, int type) {
        StringBuffer sb = new StringBuffer(sysDao.getSql(type));
        MethodParam param = new MethodParam("ByProperty", "", "", Sys.class.getName());
        String sysAll = String.valueOf(vo.getParams("sysAll"));
        if (Validator.isNull(sysAll)) {
            String str = buildParams(param, vo);
            if (Validator.isNull(str)) {
                return param;
            }
            sb.append(str);
        }
        sb.append(" order by orderNum");
        param.setSqlStr(sb.toString());
        return param;
    }

    private String buildParams(MethodParam param, ParamsVo vo) {
        StringBuffer sb = new StringBuffer();
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysCname")))) {
            sb.append(" and sysCname like :sysCname");
            param.setParams("sysCname", "'%" + String.valueOf(vo.getParams("sysCname")) + "%'");
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
            sb.append(" and sysEname like :sysEname");
            param.setParams("sysEname", "'%" + String.valueOf(vo.getParams("sysEname")) + "%'");
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("lockType")))) {
            sb.append(" and checkIp = :checkIp");
            param.setParams("checkIp", String.valueOf(vo.getParams("checkIp")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("allowIplist")))) {
            sb.append(" and allowIplist = :allowIplist");
            param.setParams("allowIplist", String.valueOf(vo.getParams("allowIplist")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysStatus")))) {
            sb.append(" and sysStatus = :sysStatus");
            param.setParams("sysStatus", String.valueOf(vo.getParams("sysStatus")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysRemark")))) {
            sb.append(" and sysRemark = :sysRemark");
            param.setParams("sysRemark", String.valueOf(vo.getParams("sysRemark")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("delStatus")))) {
            sb.append(" and delStatus = :delStatus");
            param.setParams("delStatus", String.valueOf(vo.getParams("delStatus")));
        }

        if (Validator.isNotNull(String.valueOf(vo.getParams("orgId")))) {
            sb.append(" and orgId = :orgId");
            param.setParams("orgId", String.valueOf(vo.getParams("orgId")));
        }
        return sb.toString();
    }

    public void setSysDao(IDao sysDao) {
        this.sysDao = sysDao;
    }

    public void setSysManager(SysManager sysManager) {
        this.sysManager = sysManager;
    }

}
